package com.kqkj.dyrule.marketing.controller;

import com.kqkj.dyrule.marketing.beans.EventBean;
import com.kqkj.dyrule.marketing.beans.EventCombinationCondition;
import com.kqkj.dyrule.marketing.beans.MarketingRule;
import com.kqkj.dyrule.marketing.beans.TimerCondition;
import com.kqkj.dyrule.marketing.service.TriggerModelRuleMatchServiceImpl;
import com.kqkj.dyrule.marketing.utils.EventUtil;
import org.apache.flink.api.common.state.ListState;

import java.util.List;
import java.util.Map;

public class TriggerModelRuleMatchController {

    TriggerModelRuleMatchServiceImpl triggerModelRuleMatchService;
    /**
     * 构造函数
     * @param listState flink的状态句柄
     */
    public TriggerModelRuleMatchController(ListState<EventBean> listState) throws Exception {
        triggerModelRuleMatchService = new TriggerModelRuleMatchServiceImpl(listState);
    }

    /**
     * 营销规则封装对象
     * rule{
     *     触发条件
     *     画像条件
     *     行为组合条件
     *     /* 定时条件 ；{
     *         行为组合条件
     *     } /
     * }
     */
    public boolean  ruleIsMatch(MarketingRule rule,EventBean eventBean) throws Exception {
        //用户触发事件
        boolean isTrigger = EventUtil.compare(eventBean, rule.getTriggerEventCondition());
        if (!isTrigger) return false;
        //用户画像筛查
        Map<String, String> userProfileConditions = rule.getUserProfileConditions();
        if (userProfileConditions != null && userProfileConditions.size()>0){
            boolean isUserProfile = triggerModelRuleMatchService.matchProfileCondition(rule.getUserProfileConditions(), eventBean.getKeyByValue());
            System.out.println("规则:"+rule.getRuleId()+"分组依据:"+rule.getKeyByFields()+"分组key:"+eventBean.getKeyByValue());
            if (!isUserProfile) return false;
        }

        //用户行为筛查

        List<EventCombinationCondition> eventCombinationConditionList = rule.getEventCombinationConditionList();
        if (eventCombinationConditionList != null && eventCombinationConditionList.size()>0){
            //循环遍历一次取一个事件组合条件进行计算
            for (EventCombinationCondition eventCombinationCondition : eventCombinationConditionList) {
                boolean b = triggerModelRuleMatchService.matchEventCombinationCondition(eventCombinationCondition, eventBean);

                //暂时写死（多个组合条件之间的关系是且）
                if (!b) return false;
            }
        }
        System.out.println("用户行为筛查结束"+eventBean);
        return true;

    }

    /**
     * 检查定时条件是否满足
     * @param deviceId
     * @param timerCondition
     * @param queryStartTime
     * @param queryEndTime
     * @return
     */
    public boolean isMatchTimeCondition(String deviceId,TimerCondition timerCondition,long queryStartTime,long queryEndTime) throws Exception {
        List<EventCombinationCondition> eventCombinationConditionList = timerCondition.getEventCombinationConditionList();
        for (EventCombinationCondition eventCombinationCondition : eventCombinationConditionList) {
            eventCombinationCondition.setTimeRangeStart(queryStartTime);
            eventCombinationCondition.setTimeRangeEnd(queryEndTime);
            EventBean eventBean = new EventBean();
            eventBean.setDeviceId(deviceId);
            eventBean.setTimeStamp(queryEndTime);//需要时间戳来计算分割点
            boolean b = triggerModelRuleMatchService.matchEventCombinationCondition(eventCombinationCondition, eventBean);
            //暂时写死（多个组合条件之间的关系是且）
            if (!b) return false;
        }
        return true;
    }
}
